﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IMS_Data;

namespace IMS_WEB
{
    public partial class ViewOrder : System.Web.UI.Page
    {
        public string startDate = "";
        public string endDate = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.DataSourceID = "";
            if (Page.IsPostBack)
            {
                startDate = Request.Form["startDate"];
                endDate = Request.Form["endDate"];
            }
            else
            {
                orderStatusList.DataSource = CommonData.GetStatusList();
                orderStatusList.DataBind();
            }
        }

        private List<string> GetProductList()
        {
            int count = 1;
            string key = "";
            List<string> productList = new List<string>();
            while (true)
            {
                key = string.Format("product_{0}", count++);
                string value = Request.Form[key];
                if (string.IsNullOrEmpty(value))
                {
                    break;
                }
                else
                {
                    productList.Add(value);
                }
            }

            return productList;
        }
        private void Bind(int startIndex)
        {
            List<string> productList = GetProductList();

            OrderReposity or = new OrderReposity();
            List<order> orders = or.GetOrderByContition(tbCompany.Text,
                Request.Form["startDate"],
                Request.Form["endDate"],
                orderStatusList.SelectedValue, startIndex, GridView1.PageSize, productList);

            GridView1.DataSourceID = "";

            GridView1.DataSource = orders;

            GridView1.DataBind();
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Bind(GridView1.PageIndex);
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            order c = new order();
            c.index_id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
            OrderReposity cr = new OrderReposity();
            cr.DeleteOrder(c);
            Bind(GridView1.PageIndex);
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;

            Bind(GridView1.PageIndex);
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal
                    || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[GridView1.Columns.Count - 1].Controls[0]).Attributes.Add("onclick",
                        "javascript:return confirm('你确认要删除该产品吗？')");
                }
            }
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Bind(GridView1.PageIndex);
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "编辑")
            {
                int row = Convert.ToInt32(e.CommandArgument);

                if (GridView1.Rows[row].Cells[7].Text.ToString().Trim() == CommonData.OrderInDraft)
                {
                    Response.Redirect(string.Format("~/newCheckinOrder.aspx?ID={0}", GridView1.Rows[row].Cells[0].Text.ToString().Trim()));
                }
                else
                {
                    Response.Redirect(string.Format("~/updateProductStatus.aspx?ID={0}", GridView1.Rows[row].Cells[0].Text.ToString().Trim()));
                }
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            Bind(GridView1.PageIndex);
        }
    }
}